package com.mg.plugin.agrometeorology.model;

import com.mg.model.Model;
import com.mg.model.Param;
import com.mg.model.Port;
import com.mg.pluginexample2.Model2;
import com.mg.pluginexample2.ModuleBase;

import java.util.ArrayList;

public class TemplateModel extends ModuleBase {
    public TemplateModel(int nID) {
        super(nID);
        m_alInputPort.add(new Port(this));
    }

    public String GetGroupName() {
        return "灾害制作";
    }

    public String GetName() {
        return "Temp";
    }

    public String GetDescription() {
        return "吉林灾害制作";
    }

    public boolean OnAttach(Port portFrom, Port portTo) {
        try {
            int i = FindPort(portFrom, false);
            if (i != -1) {
                return true;
            }
            i = FindPort(portTo, true);
            if (i == -1) {
                return false;
            }
            m_alParam.clear();
            portFrom.GetModule().GetOutputParam(portFrom, m_alParam);
            portTo.SetType(portFrom.GetType());
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
        return true;
    }

    public boolean OnDetach(Port portFrom, Port portTo) {
        if (!super.OnDetach(portFrom, portTo))
            return false;
        try {
            int i = FindPort(portTo, true);
            if (i == -1)
                return true;
            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        }
    }

    //用于 参数->XML 等
    public int GetParam(ArrayList<Param> alParam) {
        try {
            super.GetParam(alParam);

            Model2 model = ((Model2) m_model);

//            alParam.add(new Param("Host", m_strHost, "地址", "xxx.xxx.xxx.xxx:xxxx", "WebService", Param.EditType.Default));
//            alParam.add(new Param("UserID", m_strUserName, "用户名", "", "WebService", Param.EditType.Default));
//            alParam.add(new Param("PassWord", m_strPassword, "密码", "", "WebService", Param.EditType.Default));
//
//            alParam.add(new Param("Origin", m_strOrigin, "场信息", "", "WebService", Param.EditType.Default));
//            alParam.add(new Param("Sequence", m_strSequenceValue, "时次", "", "WebService", Param.EditType.Default));
//            alParam.add(new Param("ForecastDate", m_strDate, "预报时间", "", "WebService", Param.EditType.Default));
//            alParam.add(new Param("ForecastFileVersion", m_strForecastFileVersion, "文件版本", "", "WebService", Param.EditType.Default));
//            alParam.add(new Param("DayFormat", m_strDayFormatType, "时间偏移（天）", "", "WebService", Param.EditType.Default));
//            alParam.add(new Param("CalcDay", m_strCalcDay, "统计天数", "", "WebService", Param.EditType.Default));

        } catch (Exception ex) {
            ex.printStackTrace();
        }

        return alParam.size();
    }

    public boolean OnParamChanged(ArrayList<Param> alParam, int nIndex, Object objValue) {
        return super.OnParamChanged(alParam, nIndex, objValue);
    }

    //用于 XML->参数 等
    public int SetParam(final ArrayList<Param> alParam) {
        int i = super.SetParam(alParam);
        try {
//            m_strHost = (String) alParam.get(i++).m_objValue;
//            m_strUserName = (String) alParam.get(i++).m_objValue;
//            m_strPassword = (String) alParam.get(i++).m_objValue;
//            m_strOrigin = (String) alParam.get(i++).m_objValue;
//            m_strSequenceValue = (String) alParam.get(i++).m_objValue;
//            m_strDate = (String) alParam.get(i++).m_objValue;
//            m_strForecastFileVersion = (String) alParam.get(i++).m_objValue;
//            m_strDayFormatType = (String) alParam.get(i++).m_objValue;
//            m_strCalcDay = (String) alParam.get(i++).m_objValue;

        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return i;
    }

    public int GetOutputParam(Port port, ArrayList<Param> alParam) {
        int i = FindPort(port, false);
        if (i == -1) {
            m_model.OutputLog(Model.LogLevel.Error,"Port Is Null");
            return 0;
        }

//        alParam.add(new Param("ResultString", m_listCSV));
        return alParam.size();
    }

    @SuppressWarnings("static-access")
    public boolean Execute() {

        OnParamChanged();
        return true;
    }

    ArrayList<Param> m_alParam = new ArrayList<Param>();
}
